Skip to main content
Version: 1.0.0

tagUtils

Module Name: tagUtils

Author: Domenico Cerone Creation Date: 25/09/2025
Last Reviewer: Domenico Cerone

Purpose: Thread-safe tag creation utilities to prevent duplicate Tags documents in high-concurrency scenarios.

Core Functions​

findOrCreateTagThreadSafe(name, group, type, additionalFields, requestId)​

Thread-safe function to find or create tags with mutex synchronization.

Parameters:

  • name (string): Tag name (e.g., "BLACK", "Medium")
  • group (string): Tag group (e.g., "Frame Color", "Size")
  • type (string): Tag type (e.g., "frameColor", "size")
  • additionalFields (object): Optional fields (e.g., \{catalogRef: brandId\})
  • requestId (string): Request ID for logging

Returns: Promise<string> - Document ID of the tag

cleanupExpiredTagLocks()​

Removes expired tag creation locks (older than 10 minutes).

How It Works​

Problem: Multiple products with same tag values processed in parallel could create duplicate Tags documents.

Solution: Exclusive lock system:

  • Lock key: TAG_CREATE_{group}_{type}_{name}
  • Exponential backoff: 50ms → 1000ms max
  • Automatic cleanup after 10 minutes

Used By​

  • createProductDocument.js - 11 tag creation functions
  • createVariantDocument.js - Frame color and size tags
  • populateVariantFromSku.js - Indirect usage via above functions
  • populateVariantFromExcel.js - Indirect usage via above functions

Benefits​

  • Granular Locking: Each tag has its own lock
  • No Duplicates: Thread-safe creation prevents duplicates
  • High Performance: Tested with 500+ concurrent products